package com.puissance4;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Classe de gestion de l'activit� Scores.
 * @author Michael
 */
public class Scores extends Activity implements OnClickListener {

	public static int _background = R.drawable.puissance4_mdpi;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);
        
        // Initialisation de l'activit�
        this.initialisation();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	// Pas de menu pour l'instant
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    public void onBackPressed() {
    	
    	// On affiche l'activit� principale
		Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
		startActivity(intent);
		// On termine cette activit�
		finish();
		
    }
    
    /**
     * Fonction d'initialisation au d�marrage de l'application.
     */
    public void initialisation() {

    	TextView nomJoueur = (TextView) findViewById(R.id.titre_nom);
    	String nJ="";
    	nJ =Options.Read(this.getApplicationContext(), "pref_nom");
    	if(nJ != ""){
    		nomJoueur.setText("Score de " + nJ);
    	} else{
    		nomJoueur.setText("Score de Joueur 1");
    	}
		String score = ReadSettings(this.getApplicationContext());
		String tab[]=score.split(";");
        int win = Integer.parseInt(tab[0]);
        int lose = Integer.parseInt(tab[1].trim());
		
		TextView nbVictoire = (TextView) findViewById(R.id.nb_victoire);
		nbVictoire.setText(win+"");
		
		TextView nbDefaite = (TextView) findViewById(R.id.nb_defaite);
		nbDefaite.setText(lose+"");
		
        
        // Choisi le bon background
    	String resBackground = "";
		resBackground = Options.Read(this.getApplicationContext(), "pref_background");
		
		if(resBackground == "")
		{
			resBackground = "0";
		}
		int iB = Integer.parseInt(resBackground); 
		
		switch (iB)
		{
		case 1: 
			_background = R.drawable.puissance4_mdpi;
		break;
		case 2: 
			_background = R.drawable.tetedemort_mdpi;
		break;
		case 3: 
			_background = R.drawable.pokemon_mdpi;
		break;
		case 4: 
			_background = R.drawable.greenlanterne_mdpi;
		break;
		case 5: 
			_background = R.drawable.hellokitty_mdpi;
		break;
		case 6: 
			_background = R.drawable.mario_mdpi;
		break;
		default:
			_background = R.drawable.puissance4_mdpi;
		}
		
		LinearLayout ll = (LinearLayout) findViewById(R.id.scores);
		ll.setBackgroundResource(_background);
    }
    

	
    /**
     * Capter l'�v�nement Click sur la vue.
     */
	public void onClick(View v) {

	}
	
	/*private void demarrerScores() {
		Toast.makeText(this.getApplicationContext(), "Settings not saved",Toast.LENGTH_SHORT).show(); 
		// Afficher l'activité Scores
		Intent intentScores = new Intent(this.getApplicationContext(), Scores.class);
		startActivity(intentScores);
		// On termine cette activité
		finish();

	}*/
	
	public static void WriteSettings(Context context, String data, String actualScore){ 
        FileOutputStream fOut = null; 
        OutputStreamWriter osw = null; 
        try{ 

        	
            fOut = context.openFileOutput("settings.dat", MODE_PRIVATE); 
            osw = new OutputStreamWriter(fOut);
            String tab[]=actualScore.split(";");
            
            int win = Integer.parseInt(tab[0]);

            int lose = Integer.parseInt(tab[1].trim());
            
        	if(data == "win"){
        		++win;
        	} else {
        		++lose;
        		
        	}
        	
        	String newScore = win+";"+lose;
        	
            osw.write(newScore);
            osw.flush(); 
           //popup surgissant pour le résultat
            
            } 
            catch (Exception e) {       
                    Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
            } 
            finally {
               try { 
                      osw.close(); 
                      fOut.close(); 
                   } catch (IOException e) { 
                       Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
                   } 
            } 
       }
	
	public static String ReadSettings(Context context){ 
        FileInputStream fIn = null; 
        InputStreamReader isr = null; 
 
        char[] inputBuffer = new char[255]; 
        String data = null; 
 
        try{ 
         fIn = context.openFileInput("settings.dat");       
            isr = new InputStreamReader(fIn); 
            isr.read(inputBuffer); 
            data = new String(inputBuffer); 
            
        } 
        catch (Exception e) {       
                  Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
        }  
        finally { 
           try { 
                  isr.close(); 
                  fIn.close(); 
                  } catch (IOException e) { 
                    Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 
                  } 
        }
        return data; 
		}
	 
	
	

}
